package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.BocomConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.b2b.req.BocomB2bReq;
import com.gopay.common.domain.bank.bocom.ebank.pay.b2b.res.BocomB2bRes;

@Controller
@Scope("prototype")
public class BocomB2bServerRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(BocomB2bServerRes.class);
	
	@RequestMapping(value = "/BocomB2bServerRes")
    protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
        logger.error("--------------- BocomB2bServerRes bankback ---------------"); 
        return super.execute(request, response);
    }
	
	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		BocomB2bReq message = new BocomB2bReq();
		message.setBank(BankConstants.BANK_LOGO_BOCOM);
		String notifyMsg =(request.getParameter("notifyMsg"));
		int lastIndex =notifyMsg.lastIndexOf("|");
		String signMsg =notifyMsg.substring(lastIndex + 1, notifyMsg.length()); // 获取签名信息
		String srcMsg = notifyMsg.substring(0, lastIndex + 1); // 获取返回明文
		List list =processSrcMsg(srcMsg);
		message.setOrderId((String) list.get(1));
		message.setBankOrderId((String) list.get(1));
		message.setRetCode((String) list.get(9));
		message.setErrorMessage("");
		message.setFront(false);
		message.setAmount(new BigDecimal((String) list.get(2)));		
		message.setPlainMessage(srcMsg);
		message.setSignMessage(signMsg);
		//银行业务产品01企业网银支付
		message.setBankBusProd(BisBankBusinessProd._01);
		// 银行接口通道信息 00: 直连
        message.setBankIntrChann(BisBankInterfaceChannel._00);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request,Result result) {
		BocomB2bReq message =(BocomB2bReq) result.getModel("res");
        OrderInfo orderInfo = new OrderInfo(); 
		if(message!=null){
			if( message.getRetCode().equals("1")){
				orderInfo.setOrderId(message.getOrderId());
			    orderInfo.setAmt(message.getAmount().toString());
			    orderInfo.setNotifyNum("1");
		        orderInfo.setPayStatus("00");
		        orderInfo.setRetCode("00");
		        orderInfo.setRetMsg("");
			}else{
				 orderInfo.setOrderId(message.getOrderId());
		         orderInfo.setAmt(message.getAmount().toString());
		         orderInfo.setNotifyNum("1");
		         orderInfo.setPayStatus("01");
		         orderInfo.setRetCode("01");
		         orderInfo.setRetMsg("");
			}
			orderInfo.setBankName("bocom");
	   }
		return orderInfo;
        
	}
	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		  if(StringUtils.isEmpty(request.getParameter("notifyMsg"))){
	        	return false;
	        }
		return true;
	}
	
	/**
	 * 解析返回报文
	 * 
	 * @param srcMsg
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private List processSrcMsg(String srcMsg) {
		StringTokenizer stName = new StringTokenizer(srcMsg, "|");
		List list = new ArrayList();
		while (stName.hasMoreTokens()) {
			String value = (String) stName.nextElement();
			if (value.equals(""))
				value = "&nbsp;";
			list.add(value);
		}
		return list;
	}

}
